package com.healthyeating.manage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.healthyeating.common.core.domain.entity.SysUser;
import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.manage.domain.Consult;
import com.healthyeating.manage.domain.vo.RecipeVo;
import com.healthyeating.manage.mapper.ConsultMapper;
import com.healthyeating.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.RecipeMapper;
import com.healthyeating.manage.domain.Recipe;
import com.healthyeating.manage.service.IRecipeService;

/**
 * 食谱管理Service业务层处理
 *
 * @author 段奇乐
 * @date 2025-01-19
 */
@Service
public class RecipeServiceImpl implements IRecipeService {

    @Autowired
    private RecipeMapper recipeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private ConsultMapper consultMapper;

    /**
     * 查询食谱管理
     *
     * @param recipeId 食谱管理主键
     * @return 食谱管理
     */
    @Override
    public Recipe selectRecipeByRecipeId(Long recipeId) {
        Recipe recipe = recipeMapper.selectRecipeByRecipeId(recipeId);
        RecipeVo recipeVo = new RecipeVo();
        BeanUtil.copyProperties(recipe, recipeVo);
        if (recipe.getConsultFlag() != null && recipe.getConsultFlag() == 1) {
            Map<String, Object> _map = new HashMap<>();
            _map.put("oldRecipeId", recipe.getRecipeId());
            Consult consult = consultMapper.selectConsult(_map);
            if (consult != null) {
                recipeVo.setSuggestBody(consult.getSuggestBody());
            }
        }
        if (recipe.getConsultFlag() != null && recipe.getConsultFlag() == 2) {
            Map<String, Object> _map = new HashMap<>();
            _map.put("newRecipeId", recipe.getRecipeId());
            Consult consult = consultMapper.selectConsult(_map);
            if (consult != null) {
                recipeVo.setSuggestBody(consult.getSuggestBody());
            }
            SysUser dietitian = sysUserMapper.selectUserById(recipe.getDietitianId());
            recipeVo.setDietitianName(dietitian.getUserName());
        }
        return recipeVo;
    }

    /**
     * 查询食谱管理列表
     *
     * @param recipeVo 食谱管理
     * @return 食谱管理
     */
    @Override
    public Map<String, Object> selectRecipeList(RecipeVo recipeVo) {
        Map<String, Object> map = new HashMap<>();
        if (recipeVo.getIsVerifying() != null && recipeVo.getIsVerifying().equals("user")) {
            recipeVo.setUserId(SecurityUtils.getUserId());
        }
        if (recipeVo.getIsVerifying() != null && recipeVo.getIsVerifying().equals("suggest")) {
            recipeVo.setDietitianId(SecurityUtils.getUserId());
        }
        if (recipeVo.getIsVerifying() != null && recipeVo.getIsVerifying().equals("consult")) {
            recipeVo.setConsultFlag(1);
        }
        List<Recipe> recipeList = recipeMapper.selectRecipeList(recipeVo);
        List<RecipeVo> recipeVoList = recipeList.stream().map(recipe -> {
            RecipeVo _recipeVo = new RecipeVo();
            BeanUtil.copyProperties(recipe, _recipeVo);
            SysUser user = sysUserMapper.selectUserById(recipe.getUserId());
            _recipeVo.setUserName(user.getUserName());
            if (recipe.getConsultFlag() == 1) {
                Map<String, Object> _map = new HashMap<>();
                _map.put("oldRecipeId", recipe.getRecipeId());
                Consult consult = consultMapper.selectConsult(_map);
                if (consult != null) {
                    Recipe _recipe = recipeMapper.selectRecipeByRecipeId(consult.getNewRecipeId());
                    _recipeVo.setNewRecipe(_recipe);
                    _recipeVo.setFeedbackBody(_recipe.getFeedbackBody());
                    _recipeVo.setSuggestBody(consult.getSuggestBody());
                }
            }
            if (recipe.getConsultFlag() == 2) {
                Map<String, Object> _map = new HashMap<>();
                _map.put("newRecipeId", recipe.getRecipeId());
                Consult consult = consultMapper.selectConsult(_map);
                if (consult != null) {
                    _recipeVo.setSuggestBody(consult.getSuggestBody());
                }
                SysUser dietitian = sysUserMapper.selectUserById(recipe.getDietitianId());
                _recipeVo.setDietitianName(dietitian.getUserName());
            }
            return _recipeVo;
        }).collect(Collectors.toList());
        map.put("oldList", recipeList);
        map.put("newList", recipeVoList);
        return map;
    }

    /**
     * 新增食谱管理
     *
     * @param recipe 食谱管理
     * @return 结果
     */
    @Override
    public int insertRecipe(Recipe recipe) {
        recipe.setUserId(SecurityUtils.getUserId());
        recipe.setCreateTime(DateUtils.getNowDate());
        recipe.setVerifyFlag(0);
        recipe.setConsultFlag(0);
        recipe.setIsPass(0);
        return recipeMapper.insertRecipe(recipe);
    }

    /**
     * 修改食谱管理
     *
     * @param recipe 食谱管理
     * @return 结果
     */
    @Override
    public int updateRecipe(Recipe recipe) {
        if (recipe.getDietitianId() != null || recipe.getConsultBody() != null) {
            recipe.setConsultTime(DateUtils.getNowDate());
            recipe.setConsultFlag(1);
        }
        if (recipe.getStar() != null || recipe.getFeedbackBody() != null) {
            recipe.setIsPass(1);
        }
        return recipeMapper.updateRecipe(recipe);
    }

    /**
     * 批量删除食谱管理
     *
     * @param recipeIds 需要删除的食谱管理主键
     * @return 结果
     */
    @Override
    public int deleteRecipeByRecipeIds(Long[] recipeIds) {
        return recipeMapper.deleteRecipeByRecipeIds(recipeIds);
    }

    /**
     * 删除食谱管理信息
     *
     * @param recipeId 食谱管理主键
     * @return 结果
     */
    @Override
    public int deleteRecipeByRecipeId(Long recipeId) {
        return recipeMapper.deleteRecipeByRecipeId(recipeId);
    }

    /**
     * 批量修改食谱管理信息
     *
     * @param recipeIds 食谱管理主键
     * @return 结果
     */
    @Override
    public int batchUpdateRecipe(Long[] recipeIds) {
        return recipeMapper.batchUpdateRecipe(recipeIds);
    }
}
